<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/1
 * Time: 15:41
 */
namespace  app\blogadmin\model;

use think\Db;
use think\Model;
use think\Request;


class Admin extends Model{
    protected $_table='admin';

    /**
     * 获取登录者的信息
     * @param $data
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLoginMember($data){
        $res=Db::table($this->_table)->where($data)->find();

        if(!empty($res)){
            return ['code'=>'1','res'=>$res];
        }else{
            return ['code'=>'-1'];
        }
    }

    /**
     * 获取所有的用户数据
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAllAdmin(){
        $res=Db::table($this->_table)->alias("a")->join("admin_role r","a.role_id=r.id",'left') ->order("a.id  ASC")->field("a.id,a.username,a.role_id,r.name,a.phone,a.email,a.last_ip,a.login_time,a.status")->select();
        return $res;
    }

    /**
     * 添加数据
     * @param $data
     * @return bool
     */
    public function add($data){
        $res=Db::table($this->_table)->insert($data);
        if($res)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * 根据条件查找一条数据
     * @param $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function  getOne($data){
        $res=Db::table($this->_table)->where($data)->field('id,username,role_id,phone,email,last_ip,login_time,status')->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    /**
     * 根据id 删除数据
     * @param $id
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del($id){
        $res=Db::table($this->_table)->delete($id);
        if($res)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public function upda($data){
        $res=Db::table($this->_table)->update($data);
        if($res){
            return $res;
        }else{
            return false;
        }
    }






}